//
//  PickerViewController.swift
//  UI
//
//  Created by admin on 14/9/22.
//  Copyright (c) 2014年 sinosun. All rights reserved.
//
/*
@availability(iOS, introduced=2.0)
class UIPickerView : UIView, NSCoding, UITableViewDataSource, NSObjectProtocol {

unowned(unsafe) var dataSource: UIPickerViewDataSource? // default is nil. weak reference
unowned(unsafe) var delegate: UIPickerViewDelegate? // default is nil. weak reference


var showsSelectionIndicator: Bool // default is NO

// info that was fetched and cached from the data source and delegate
var numberOfComponents: Int { get }

func numberOfRowsInComponent(component: Int) -> Int

func rowSizeForComponent(component: Int) -> CGSize


// returns the view provided by the delegate via pickerView:viewForRow:forComponent:reusingView:
// or nil if the row/component is not visible or the delegate does not implement
// pickerView:viewForRow:forComponent:reusingView:
func viewForRow(row: Int, forComponent component: Int) -> UIView?


// Reloading whole view or single component
func reloadAllComponents()

func reloadComponent(component: Int)

// selection. in this case, it means showing the appropriate row in the middle
func selectRow(row: Int, inComponent component: Int, animated: Bool) // scrolls the specified row to center.

func selectedRowInComponent(component: Int) -> Int // returns selected row. -1 if nothing selected
}



protocol UIPickerViewDataSource : NSObjectProtocol {

// returns the number of 'columns' to display.
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int

// returns the # of rows in each component..
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
}




protocol UIPickerViewDelegate : NSObjectProtocol {

// returns width of column and height of row for each component.
optional func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat
optional func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat

// these methods return either a plain NSString, a NSAttributedString, or a view (e.g UILabel) to display the row for the component.
// for the view versions, we cache any hidden and thus unused views and pass them back for reuse.
// If you return back a different object, the old one will be released. the view will be centered in the row rect
optional func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!
@availability(iOS, introduced=6.0)
optional func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? // attributed title is favored if both methods are implemented
optional func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView

optional func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
}

*/
import UIKit

class PickerViewController: UIViewController, UIPickerViewDataSource ,UIPickerViewDelegate{
    var names : [[String]] = [ name_controllers, name_tools, name_views]

    @IBOutlet var pickerView: UIPickerView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
//        self.pickerView.dataSource = self
//        self.pickerView.delegate = self
     //   self.pickerView.backgroundColor = UIColor.grayColor()
        
       // NSTimer.scheduledTimerWithTimeInterval(1.0, invocation: NSInvocation(), repeats: true)
          NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "time", userInfo: nil, repeats: true)
        
        
    }
    
    func time(){
        println("----------------")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //MARK: - UIPickerViewDataSource
    // returns the number of 'columns' to display.
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
        return names.count
    }
    
    // returns the # of rows in each component..
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
        return names[component].count
    }
    
    //MARK: -UIPickerViewDelegate
    
    
    // MARK: - Navigation
    
    func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat{
        return 100.0
    }
    func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat{
        return 40.0
    }
    
    // these methods return either a plain NSString, a NSAttributedString, or a view (e.g UILabel) to display the row for the component.
    // for the view versions, we cache any hidden and thus unused views and pass them back for reuse.
    // If you return back a different object, the old one will be released. the view will be centered in the row rect
//    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!{
//        return names[component][row]
//    }
//    @availability(iOS, introduced=6.0)
//    func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
//        
//    }// attributed title is favored if both methods are implemented
//    
    func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView{
        var labelView = UILabel()
        labelView.text = names[component][row]
        return labelView
    }
    
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
        println("value=\(names[component][row])")
    }

    // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    

}
